package com.dragon.client.controller.contract;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.client.config.annotation.AddPage;
import com.dragon.client.config.annotation.Log;
import com.dragon.client.config.annotation.Login;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.contract.ContractService;
import com.dragon.logical.service.contract.ProduceService;
import com.dragon.repository.domain.dto.OtherFileDTO;
import com.dragon.repository.domain.dto.ProduceDTO;
import com.dragon.repository.domain.vo.ProduceProductsVO;
import com.dragon.repository.domain.vo.ProduceVO;
import com.dragon.repository.entity.OtherFile;
import com.dragon.repository.entity.Produce;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 生产管理
 * @author Charlotte
 */
@RestController
@Api(tags = "生产单")
@RequestMapping("produce")
public class ProduceController {

    @Autowired
    ProduceService service;

    @GetMapping("list")
    @ApiOperation("生产单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "search", value = "搜索关键字", required = false, defaultValue = "", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true, type = "int"),
            @ApiImplicitParam(name = "type", value = "生产状态", required = false, type = "int"),
            @ApiImplicitParam(name = "status", value = "审核状态", required = false, type = "int"),
            @ApiImplicitParam(name = "customerName", value = "客户名称", required = false, type = "string"),
            @ApiImplicitParam(name = "businessAdminId", value = "业务员id", required = false, type = "int")
    })
    public ResponseData<List<ProduceDTO>> selectProduce(@RequestParam(required = false) String search,
                                                        @RequestParam Integer page, @RequestParam Integer limit,
                                                        @RequestParam(required = false) Integer type,
                                                        @RequestParam(required = false) Integer status,
                                                        @RequestParam(required = false) String customerName,
                                                        @RequestParam(required = false) Integer businessAdminId,
                                                        String startTime, String endTime){
        Page<Produce> producePage = service.listProduce(search, page, limit,type,status, customerName, businessAdminId, startTime, endTime);
        return ResponseData.success(ProduceDTO.convertList(producePage.getRecords()), producePage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation("添加生产单")
    @Log
    public ResponseData addProduce( @RequestBody @Validated(Insert.class) ProduceVO vo){
        Produce produce = ProduceVO.convert(vo);
        produce.setProduceProducts(ProduceProductsVO.convertList(vo.getProduceProductsVOS()));
        service.addProduce(produce);
        return ResponseData.success();
    }

    @PostMapping("update")
    @ApiOperation("更改生产单")
    @Log
    public ResponseData updateProduce(@RequestBody @Validated(Update.class) ProduceVO vo){
        service.updateProduce(ProduceVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除生产单")
    @Log
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "生产单的id集合", required = true, type = "string"),
            @ApiImplicitParam(name = "projectId", value = "项目id", required = false, type = "int")
    })
    public ResponseData deleteProduce(@RequestParam List<Integer> ids, @RequestParam(required = false) Integer projectId) {
        service.deleteProduce(ids);
        return ResponseData.success();
    }

    @PostMapping("listById")
    @ApiOperation("根据id查看详情")
    public ResponseData selectProduceById(@RequestParam
                                          @ApiParam(name = "id", value = "id",required = true, type = "int") Integer id){
        return ResponseData.success(service.selectProduceById(id));
    }


    @PostMapping("changeProduceType")
    @ApiOperation("修改生产状态")
    public ResponseData changeProduceType(Integer id, Integer type){
        service.changeProduceType(id,type);
        return ResponseData.success();
    }

    @GetMapping("getAddress")
    @ApiOperation(value = "根据生产单id获取客户地址")
    public ResponseData getAddress(
            @ApiParam(name = "produceId", value = "生产单id", required = true, type = "int")
            @RequestParam Integer produceId) {
        return ResponseData.success(service.getAddress(produceId));
    }

    @GetMapping("statistics")
    @ApiOperation(value = "生产统计信息")
    public ResponseData ProductionStatistics(){
        return ResponseData.success(service.statistics());
    }

    @GetMapping("informationOfProduce")
    @ApiOperation(value = "合同总信息之生产")
    public ResponseData<ProduceDTO> informationOfProduce(
            @ApiParam(name = "contractId", value = "合同id", required = true, type = "int")
            @RequestParam Integer contractId){
        Produce produces = service.selectProduceByContractId(contractId);
        return ResponseData.success(ProduceDTO.convert(produces));
    }

    @GetMapping("getContractOtherFile")
    @ApiOperation(value = "获取合同附件")
    public ResponseData<List<OtherFileDTO>> getContractOtherFileByProduceId(
            @ApiParam(name = "produceId", value = "生产单id", required = true, type = "int")
            @RequestParam Integer produceId
    ){
        List<OtherFile> otherFileList = service.getContractOtherFileByProduceId(produceId);
        return ResponseData.success(OtherFileDTO.convertList(otherFileList));
    }
}
